package com.lizardmind.haofudoctor.ui.studio.serviceSetting

import android.arch.lifecycle.ViewModelProviders
import android.os.Bundle
import android.support.constraint.ConstraintLayout
import android.view.Gravity
import android.view.View
import android.widget.CheckBox
import android.widget.LinearLayout
import android.widget.TextView
import com.bigkoo.pickerview.TimePickerView
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.SizeUtils
import com.linxiao.framework.activity.BaseActivity
import com.linxiao.framework.util.color
import com.linxiao.framework.util.ke
import com.lizardmind.haofudoctor.R
import com.lizardmind.haofudoctor.manager.postApi
import com.lizardmind.haofudoctor.other.DoctorInfo
import com.lizardmind.haofudoctor.other.Request
import com.lizardmind.haofudoctor.other.formatToZY
import com.lizardmind.haofudoctor.other.jsonOf
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.include_title_right_text.*
import org.jetbrains.anko.*
import org.jetbrains.anko.sdk25.coroutines.onClick
import org.json.JSONObject
import java.text.SimpleDateFormat
import java.util.*

/**
 * 服务时间设置
 * Created by Extends on 2017/11/27 16:26
 */
class TimeSettingActivity:BaseActivity() {
    private val vm by lazy { ViewModelProviders.of(ActivityUtils.getActivityList()[ActivityUtils.getActivityList().lastIndex-1] as BaseActivity).get(ServiceSettingVM::class.java) }
    private val array = arrayOf("周一","周二","周三","周四","周五","周六","周日")
    private val week by lazy { intent.getStringExtra("week") }
    private val time by lazy {intent.getStringExtra("time")?:""}
    private var startDefault = "09:30"
    private var endDefault = "18:00"
    private lateinit var startView:TextView
    private lateinit var endView:TextView
    private val arrayCheck = arrayOfNulls<CheckBox>(7)
    private val formatter  = SimpleDateFormat("HH:mm")
    private val startTime by lazy {
        TimePickerView.Builder(this){ date, _ ->
            startView.text = formatter.format(date)
        }.setType(booleanArrayOf(false,false,false,true,true,false))
                .build()
    }
    private val endTime by lazy {
        TimePickerView.Builder(this){ date, _ ->
            endView.text = formatter.format(date)
        }.setType(booleanArrayOf(false,false,false,true,true,false))
                .build()
    }
    override fun onCreatRootView(): View? {
        ke(intent.getStringExtra("time"),intent.getStringExtra("week"))
        return linearLayout {
            orientation = LinearLayout.VERTICAL
            include<ConstraintLayout>(R.layout.include_title_right_text)
            linearLayout {
                orientation = LinearLayout.HORIZONTAL
                gravity = Gravity.CENTER_VERTICAL
                backgroundColor = color(R.color.white)
                horizontalPadding = SizeUtils.dp2px(16f)
                textView("开始时间").lparams(-2,-2,1f)
                startView = textView(startDefault)
                onClick { startTime.show() }
            }.lparams(-1,SizeUtils.dp2px(54f))
            include<View>(R.layout.include_split_line)
            linearLayout {
                orientation = LinearLayout.HORIZONTAL
                gravity = Gravity.CENTER_VERTICAL
                backgroundColor = color(R.color.white)
                horizontalPadding = SizeUtils.dp2px(16f)
                textView("结束时间").lparams(-2,-2,1f)
                endView = textView(endDefault)
                onClick { endTime.show() }
            }.lparams(-1,SizeUtils.dp2px(54f))
            space().lparams(-1,SizeUtils.dp2px(10f))
            linearLayout {
                orientation = LinearLayout.VERTICAL
                horizontalPadding = SizeUtils.dp2px(25f)
                backgroundColor = color(R.color.white)
                array.forEachIndexed { i, it ->
                    arrayCheck[i] = checkBox(it){
                        gravity = Gravity.RIGHT or Gravity.CENTER_VERTICAL
                        verticalPadding = SizeUtils.dp2px(8f)
                        isChecked = week.indexOf(it)>=0
                    }
                }
            }
        }
    }

    override fun onInitView(savedInstanceState: Bundle?) {
        //标题栏初始化
        imgLeft.setImageResource(R.mipmap.back)
        imgLeft.onClick { finish() }
        titleText.text = "服务时间设置"
        tvRight.text = "保存"
        tvRight.onClick {
            val time = startView.text.toString()+"-"+endView.text
            val week = arrayCheck.filter { it!=null&&it.isChecked }.joinToString(" ") { it!!.text }
            observe {
                postApi<Request<String>>(hashMapOf(
                        "action" to "edit_service",
                        "token" to DoctorInfo.token,
                        "data" to jsonOf(
                                "time" to time,
                                "week" to week
                        ).formatToZY()

                ), Consumer {
                    it.dispose {
                        toast(it)
                        vm.time.set(time)
                        vm.week.set(week)
                    }
                })
            }
        }


        //设置默认时间
        if(time.isNotEmpty()){
            startDefault = time.split("-")[0]
            endDefault = time.split("-")[1]
            startView.text = startDefault
            endView.text = endDefault
        }

        val date = Calendar.getInstance()
        date.set(1970,0,1,startDefault.split(":")[0].toInt(),startDefault.split(":")[1].toInt())
        startTime.setDate(date)
        date.set(1970,0,1,endDefault.split(":")[0].toInt(),endDefault.split(":")[1].toInt())
        endTime.setDate(date)


    }
}